home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Programming / AmigaTalk / general / Dictionary.st < prev    next >
Text File  |  2000-02-13  |  3KB  |  98 lines

  1. "--------------------------------------------------------------"
  2. "  Dictionaries are implemented using Points in order to reduce
  3.    the number of classes in the standard prelude
  4.    This also has the advantage of making the output appear in
  5.       'key @ value'    form"
  6. "--------------------------------------------------------------"
  7.  
  8. Class Dictionary :KeyedCollection
  9. ! hashTable currentBucket currentList !
  10. [
  11.    new
  12.       hashTable <- Array new: 17
  13. |
  14.    hashNumber: aKey
  15.       ^ ( <primitive 5 aKey> \\ hashTable size) + 1
  16. |
  17.    getList: aKey      ! list bucketNumber !
  18.       bucketNumber <- self hashNumber: aKey.
  19.       list <- hashTable at: bucketNumber.
  20.  
  21.       (list isNil)
  22.          ifTrue: [list <- List new.
  23.              hashTable at: bucketNumber put: list].
  24.  
  25.       ^ list
  26. |
  27.    at: aKey put: anObject   ! list anAssoc !
  28.       list <- self getList: aKey.
  29.       anAssoc <- self findAssociation: aKey inList: list.
  30.  
  31.       (anAssoc isNil)
  32.          ifTrue:  [anAssoc <- (Point new x: aKey) y: anObject.
  33.                    list add: anAssoc]
  34.          ifFalse: [anAssoc y: anObject].
  35.  
  36.       ^ anObject
  37. |
  38.    at: aKey ifAbsent: exceptionBlock   ! list anAssoc ! 
  39.       list <- self getList: aKey.
  40.       anAssoc <- self findAssociation: aKey inList: list.
  41.  
  42.       (anAssoc isNil)
  43.          ifTrue: [^ exceptionBlock value].
  44.  
  45.       ^ anAssoc y
  46. |
  47.    removeKey: aKey ifAbsent: exceptionBlock    ! list anAssoc !
  48.       list <- self getList: aKey.
  49.       anAssoc <- self findAssociation: aKey inList: list.
  50.  
  51.       (anAssoc isNil)
  52.          ifTrue: [^ exceptionBlock value].
  53.  
  54.       ^ ( list remove: anAssoc 
  55.           ifAbsent: [ ^ exceptionBlock value ] ) y
  56. |
  57.    findAssociation: aKey inList: linkedList
  58.       linkedList do: 
  59.          [:item | (item x = aKey) ifTrue: [^ item]].
  60.       ^ nil
  61. |
  62.    first ! item !
  63.       (1 to: 17) do: 
  64.          [:i | ((item <- self checkBucket: i) notNil)
  65.              ifTrue: [ ^ item y] ] .
  66.       ^ nil
  67. |
  68.    next         ! item !
  69.       ((item <- currentList next) notNil)
  70.          ifTrue: [ ^ item y ].
  71.  
  72.       [currentBucket < 17] whileTrue:
  73.          [currentBucket <- currentBucket + 1.
  74.           ((item <- self checkBucket: currentBucket) notNil)
  75.             ifTrue: [ ^ item y ] ].
  76.       ^ nil
  77. |
  78.    printString
  79.       ^ (self inject: (self class printString) , ' ( '
  80.                 into: [ :aString :aValue |
  81.                         aString , self currentKey printString ,
  82.                         ' @ ' , aValue printString , ' ' ]
  83.                 ) , ')'
  84. |
  85.    currentKey   ! clist !
  86.       ^ (currentList notNil) 
  87.          ifTrue: [clist <- currentList current.
  88.              (clist notNil) ifTrue: [clist x]
  89.                  ]
  90. |
  91.    checkBucket: bucketNumber
  92.       ((currentList <- hashTable at: 
  93.             (currentBucket <- bucketNumber)) isNil)
  94.          ifTrue: [ ^ nil ].
  95.  
  96.       ^ currentList first
  97. ]
  98.